package com.sea.base.permission

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.provider.Settings


/**
 * @Description 跳转权限设置页
 *
 * HUAWEI 跳转权限设置需要系统权限 目前暂时没有其他方式
 * vivo  兼容测试通过10台有6台通过 剩余4台系统版本为8.0之前未通过
 * OPPO  暂时无法跳转需要oppo权限，已向开发者平台进行提交工单询问，暂时还未回复
 * xiaomi  可以适配
 * Meizu  可以适配
 * samsung 暂未处理
 * 最新参考链接：[https://blog.csdn.net/L_201607/article/details/122963184]
 */

object PermissionDetailManager {
    fun toSettingPermission(context: Context) {
        when (Build.MANUFACTURER) {
            "HUAWEI" ->
                goHuaWeiManager(context)

            "vivo" ->
                goVivoManager(context)

            "OPPO" ->
                goOppoManager(context)

            "Meizu" ->
                goMeiZuManager(context)

            "Xiaomi" ->
                goXiaoMiManager(context)

            "samsung" ->
                goSangXinManager(context)

            else ->
                goIntentSetting(context)
        }

    }

    private fun goSangXinManager(context: Context) {
        goIntentSetting(context)
    }

    private fun goXiaoMiManager(context: Context) {
        val intent = Intent()
        intent.action = "miui.intent.action.APP_PERM_EDITOR"
        intent.setClassName(
            "com.miui.securitycenter",
            "com.miui.permcenter.permissions.AppPermissionsEditorActivity"
        )
        intent.putExtra("extra_pkgname", context.packageName)
        if (context.packageManager.resolveActivity(intent, 0) != null) {
            safeStartActivity(intent, context)
            return
        }
        intent.setClassName(
            "com.miui.securitycenter",
            "com.miui.permcenter.permissions.PermissionsEditorActivity"
        )
        if (context.packageManager.resolveActivity(intent, 0) != null) {
            safeStartActivity(intent, context)
            return
        }
        goIntentSetting(context)
    }


    private fun goMeiZuManager(context: Context) {
        val intent = Intent("com.meizu.safe.security.SHOW_APPSEC")
        intent.addCategory(Intent.CATEGORY_DEFAULT)
        intent.putExtra("packageName", context.packageName)
        if (context.packageManager.resolveActivity(intent, 0) != null) {
            safeStartActivity(intent, context)
            return
        } else {
            goIntentSetting(context)
        }
    }

    private fun goVivoManager(context: Context) {
        val intent = Intent("")
        intent.addCategory(Intent.CATEGORY_DEFAULT)
        intent.putExtra("packageName", context.packageName)
        intent.data = Uri.parse("package:${context.packageName}")
        intent.setClassName(
            "com.vivo.permissionmanager",
            "com.vivo.permissionmanager.activity.PurviewTabActivity"
        )
        if (context.packageManager.resolveActivity(intent, 0) != null) {
            safeStartActivity(intent, context)
        } else {
            goIntentSetting(context)
        }
    }

    private fun goOppoManager(context: Context) {
        val intent = Intent()
        intent.putExtra("packageName", context.packageName)
        intent.setClassName(
            "com.coloros.securitypermission",
            "com.coloros.securitypermission.permission.PermissionGroupsActivity"
        )
        if (context.packageManager.resolveActivity(intent, 0) != null) {
            safeStartActivity(intent, context)
        } else {
            goIntentSetting(context)
        }
    }


    private fun goHuaWeiManager(context: Context) {
        goIntentSetting(context)
    }

    private fun goIntentSetting(context: Context) {
        context.startActivity(
            Intent(
                Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
                Uri.parse("package:${context.packageName}")
            )
        )
    }


    private fun safeStartActivity(intent: Intent, context: Context) {
        try {
            context.startActivity(intent)
        } catch (e: Exception) {
            e.printStackTrace()
            goIntentSetting(context)
        }
    }

}